package com.mx.zb.mng.ctrl;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.mx.zb.mng.bo.Agent;
import com.mx.zb.mng.bo.AgentLevel;
import com.mx.zb.mng.dao.AgentLevelMapper;
import com.mx.zb.mng.dao.AgentMapper;
import com.mx.zb.mng.utils.AjaxResult;
import com.mx.zb.mng.utils.context.ContextThread;
import com.mx.zb.mng.utils.db2.Page;
import com.mx.zb.mng.utils.db2.PageHelper;

/**
 * 
 * @ClassName: AgentLevelRestCtrl
 * @Description: 代理商级别
 * @author 李志伟
 * @date 2017年9月25日 下午10:11:12 
 *
 */
@RestController
@RequestMapping("/a/biz")
public class AgentLevelRestCtrl {
	

	@Autowired
	AgentLevelMapper agentLevelMapper;
	
	@Autowired
	AgentMapper agentMapper;
	
	Logger log = LoggerFactory.getLogger(this.getClass());
	
	
	@RequestMapping(value= {"/alevel"}, method= {RequestMethod.PUT})
	@Transactional
	public AjaxResult save(@RequestBody AgentLevel o) {
//		o.setId(UnionCode.getShortUuid());
		AgentLevel a=agentLevelMapper.findByName(o);
		if(a!=null) {
			return new AjaxResult().failure("级别名称不能重复，请修改");
		}
		
		o.setCreateDate(new Date());
		o.setUpdateDate(o.getCreateDate());
		o.setCreateOper(ContextThread.get().getOperatorId());
		o.setUpdateOper(ContextThread.get().getOperatorId());
		agentLevelMapper.save(o);

		return new AjaxResult().success();
	}
	
	@RequestMapping(value= {"/alevel"}, method= {RequestMethod.POST})
	@Transactional
	public AjaxResult update(@RequestBody AgentLevel o) {
		AgentLevel a=agentLevelMapper.findByName(o);
		if(a!=null) {
			return new AjaxResult().failure("级别名称不能重复，请修改");
		}
		o.setUpdateDate(new Date());
		o.setUpdateOper(ContextThread.get().getOperatorId());

		agentLevelMapper.updateByPrimaryKeySelective(o);
		
		return new AjaxResult().success();
	}
	
	@RequestMapping(value= {"/alevel"}, method= {RequestMethod.DELETE})
	@Transactional
	public AjaxResult delete(@RequestBody AgentLevel o) {
		Agent pojo=new Agent();
		pojo.setLevelId(o.getId());
		List<Agent> list=agentMapper.findAgentList(pojo);
		if(list!=null&&list.size()>0) {
			return new AjaxResult().failure("该级别已被使用，不能删除");
		}
		agentLevelMapper.deleteByPrimaryKey(o.getId());
		return new AjaxResult().success();
	}
	
	@RequestMapping(value= {"/alevels"}, method= {RequestMethod.GET})
	public AjaxResult getList() {
		PageHelper.startPage();
		AgentLevel o = new AgentLevel();
		String search = ContextThread.get().getSearch();
		if(search!=null&&search!="") {
			try{
				o= JSON.parseObject(search,AgentLevel.class) ;
				if(o.getUpdateDate()!=null) {
					Calendar c=Calendar.getInstance();  
					c.setTime(o.getUpdateDate());
					c.add(Calendar.DATE, 1); 
					o.setUpdateDate(c.getTime());
				 }
			
				}catch(Exception e) {
					e.printStackTrace();
					o = new AgentLevel();
			}
		}
		 
		
		Page<AgentLevel> pages = agentLevelMapper.queryPage(o);
		
		return new AjaxResult().success(pages);
	}
	
	
	@RequestMapping(value= {"/alevel/obj"}, method= {RequestMethod.POST})
	public AjaxResult getObject(@RequestBody AgentLevel o) {
		AgentLevel pojo=agentLevelMapper.selectByPrimaryKey(o.getId());

		return new AjaxResult().success(pojo);
	}
	
	@RequestMapping(value= {"/alevel/edit"}, method= {RequestMethod.GET})
	public String toEidt(Model model,@RequestParam String id) {
		if(id!=null&&!"".equals(id)&&!"null".equals(id)) {
			AgentLevel pojo=agentLevelMapper.selectByPrimaryKey(new Integer(id));
			model.addAttribute("entity",pojo);
			
		}else {
			model.addAttribute("entity",new AgentLevel() );
		}
		return "agent_edit";
	}
	
	
	@RequestMapping(value= {"/alevel/getObj"}, method= {RequestMethod.GET})
	public AjaxResult getObject(Model model,@RequestParam String id) {
		AgentLevel pojo=new AgentLevel();
		if(StringUtils.hasText(id)) {
			 pojo=agentLevelMapper.selectByPrimaryKey(new Integer(id));
			}
			return new AjaxResult().success(pojo);
	}
	
}
